package com.xiaoqiang.mybatis.study;

import com.xiaoqiang.mybatis.study.base.Student;
import com.xiaoqiang.mybatis.study.dao.StudentMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileNotFoundException;
import java.io.FileReader;

public class XMLConfigDemo {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(new FileReader(XMLConfigDemo.class.getClassLoader().getResource("").getPath() + "mybatis-config.xml"));
        } catch (FileNotFoundException e) {
            sqlSessionFactory = null;
        }
    }

    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }

    public static void main(String[] args) {
        insert();
    }

    public static void insert(){
        try(SqlSession sqlSession = getSqlSession()){
            Student student = new Student();
            student.setName("赵六");
            student.setAge(18);
            StudentMapper  mapper = sqlSession.getMapper(StudentMapper.class);
            System.out.println(mapper.insert(student));
            System.out.println(student);
        }
    }
}
